//
//  MainViewController.m
//  MiaoTu
//
//  Created by dllo on 16/8/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MainViewController.h"
#import "TravelViewController.h"
#import "MineViewController.h"
#import "LoginTransitionViewController.h"
#import "Login.h"
#import "LocalViewController.h"
#import "MessageViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "MyLocation.h"


@interface MainViewController () <UITabBarControllerDelegate, CLLocationManagerDelegate>
@property (nonatomic, assign) BOOL isLogin;
@property (nonatomic, strong) CLLocationManager *locManager;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 创建一系列的ViewController
    TravelViewController *travelVC = [[TravelViewController alloc] init];
    UINavigationController *travelNaVC = [[UINavigationController alloc] initWithRootViewController:travelVC];
    travelNaVC.tabBarItem = [self barItemWithTitle:@"旅行" normalImageName:@"travel_normal" selectedImageName:@"travel_selected"];
    
    LocalViewController *localVC = [[LocalViewController alloc] init];
    UINavigationController *localNaVC = [[UINavigationController alloc] initWithRootViewController:localVC];
    localNaVC.tabBarItem = [self barItemWithTitle:@"当地" normalImageName:@"local_normal" selectedImageName:@"local_selected"];
    
    MessageViewController *messageVC = [[MessageViewController alloc] init];
    UINavigationController *messageNaVC = [[UINavigationController alloc] initWithRootViewController:messageVC];
    messageNaVC.tabBarItem = [self barItemWithTitle:@"消息" normalImageName:@"message_normal" selectedImageName:@"message_selected"];
    
    MineViewController *mineVC = [[MineViewController alloc] init];
    mineVC.login = [[NSUserDefaults standardUserDefaults] valueForKey:@"loginModel"];
    UINavigationController *mineNaVC = [[UINavigationController alloc] initWithRootViewController:mineVC];
    mineNaVC.tabBarItem = [self barItemWithTitle:@"我的" normalImageName:@"mine_normal"  selectedImageName:@"mine_selected"];
   
    self.tabBar.tintColor = [UIColor colorWithRed:0.827 green:0.443 blue:0.000 alpha:1.000];
    self.viewControllers = @[travelNaVC, localNaVC, messageNaVC, mineNaVC];
    self.delegate = self;
    
    self.selectedIndex = 0;
    
    [self ceateLocationManager];
}

- (void)ceateLocationManager {
    self.locManager = [CLLocationManager new];
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"定位服务还未打开");
        return;
    }
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
        [_locManager requestWhenInUseAuthorization];
    } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) {
        
        self.locManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locManager.distanceFilter = 50;
        self.locManager.delegate = self;
        [self.locManager startUpdatingLocation];
    }
}

#pragma mark - CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation *location = [locations firstObject];
    [MyLocation sharedMyLocation].location = location;
    [_locManager stopUpdatingLocation];
}


- (UITabBarItem *)barItemWithTitle:(NSString *)title
                   normalImageName:(NSString *)normalImageName
                 selectedImageName:(NSString *)selectedImageName {
    return [[UITabBarItem alloc] initWithTitle:title image:[[UIImage imageNamed:normalImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
}

#if 1
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    self.isLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"isLogin"];
    if (!_isLogin && [viewController.tabBarItem.title isEqualToString:@"我的"]) {
        LoginTransitionViewController *logTranVC = [[LoginTransitionViewController alloc] initWithNibName:@"LoginTransitionViewController" bundle:[NSBundle mainBundle]];
        UINavigationController *loginNaVC = [[UINavigationController alloc] initWithRootViewController:logTranVC];
        [self presentViewController:loginNaVC animated:YES completion:^{

        }];
        
        return NO;
    }
    return YES;
}
#endif

//- (void)isLoginNotification:(NSNotification *)notification {
//    self.isLogin = notification.userInfo[@"isLogin"];
//    self.mineVC.login = (Login *)notification.userInfo[@"loginModel"];
//}
//
//- (void)dealloc {
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"isLoginNotification" object:nil];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
